## install.packages("Matching")
## install.packages("rgenoud")

library(Matching)
library(rgenoud)

matching <- read.csv("~/Dropbox/switchers/Matching/data/contribDB.csv")
matching <- transform(matching, id = as.numeric(factor(bonica_rid)))
attach(matching)

## Setting the covariates to (1) match on; and (2) obtain balance on:

covariates_match <- cbind(switcher_code, post_dem, dwnom1, seniority, dpres, les, votepct, pre_party, pre_individual, pre_business, pre_labor, pre_ideological, pre_other, general_length)

covariates_balance <- cbind(switcher_code, post_dem, dwnom1, seniority, dpres, les, votepct, pre_party, pre_individual, pre_business, pre_labor, pre_ideological, pre_other, general_length)

## Performing the match -- 1-to-1 WITH replacement:

	## Finding balance -- 
match_1 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 1, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 270, unif.seed = 492, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
party_1 <- Match(Y = post_party, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1)
summary(party_1)

business_1 <- Match(Y = post_business, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = party_1)
summary(business_1)

individual_1 <- Match(Y = post_individual, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = party_1)
summary(individual_1)

labor_1 <- Match(Y = post_labor, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = party_1)
summary(labor_1)

ideological_1 <- Match(Y = post_ideological, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = party_1)
summary(ideological_1)

other_1 <- Match(Y = post_other, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = party_1)
summary(other_1)

	## Checking balance 
balance_1 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_party + pre_individual + pre_business + pre_labor + pre_ideological + pre_other +
general_length, match.out = party_1, nboots = 5000)

	## Generating the matched dataset --
treated_1 <- matching[party_1$index.treated,]
treated_1$pair_id <- treated_1$id
control_1 <- matching[party_1$index.control,]
control_1$pair_id <- treated_1$id

matched_sources_1 <- rbind(treated_1, control_1)
matched_sources_1 <- matched_sources_1[order(matched_sources_1$pair_id),]
matched_sources_1$match <- 1

## Performing the match -- 1-to-2 WITH replacement:

	## Finding balance -- 
match_2 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 2, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 324, unif.seed = 217, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
party_2 <- Match(Y = post_party, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2)
summary(party_2)

business_2 <- Match(Y = post_business, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = party_2)
summary(business_2)

individual_2 <- Match(Y = post_individual, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = party_2)
summary(individual_2)

labor_2 <- Match(Y = post_labor, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = party_2)
summary(labor_2)

ideological_2 <- Match(Y = post_ideological, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = party_2)
summary(ideological_2)

other_2 <- Match(Y = post_other, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = party_2)
summary(other_2)

	## Checking balance 
balance_2 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_party + pre_individual + pre_business + pre_labor + pre_ideological + pre_other + general_length, match.out = party_2, nboots = 5000)

	## Generating the matched dataset --
treated_2 <- matching[party_2$index.treated,]
treated_2$pair_id <- treated_2$id
control_2 <- matching[party_2$index.control,]
control_2$pair_id <- treated_2$id

matched_sources_2 <- rbind(treated_2, control_2)
matched_sources_2 <- matched_sources_2[order(matched_sources_2$pair_id),]
matched_sources_2 <- matched_sources_2[!duplicated(matched_sources_2), ]
matched_sources_2$match <- 2

## Performing the match -- 1-to-3 WITH replacement:

	## Finding balance -- 
match_3 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 3, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 723, unif.seed = 659, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
party_3 <- Match(Y = post_party, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3)
summary(party_3)

business_3 <- Match(Y = post_business, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = party_3)
summary(business_3)

individual_3 <- Match(Y = post_individual, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = party_3)
summary(individual_3)

labor_3 <- Match(Y = post_labor, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = party_3)
summary(labor_3)

ideological_3 <- Match(Y = post_ideological, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = party_3)
summary(ideological_3)

other_3 <- Match(Y = post_other, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = party_3)
summary(other_3)

	## Checking balance 
balance_3 <- MatchBalance(switcher ~ switcher_code + post_dem + dwnom1 + seniority + dpres + les + votepct + pre_party + pre_individual + pre_business + pre_labor + pre_ideological + pre_other + general_length, match.out = party_3, nboots = 5000)

	## Generating the matched dataset --
treated_3 <- matching[party_3$index.treated,]
treated_3$pair_id <- treated_3$id
control_3 <- matching[party_3$index.control,]
control_3$pair_id <- treated_3$id

matched_sources_3 <- rbind(treated_3, control_3)
matched_sources_3 <- matched_sources_3[order(matched_sources_3$pair_id),]
matched_sources_3 <- matched_sources_3[!duplicated(matched_sources_3), ]
matched_sources_3$match <- 3

sources_postdem <- rbind(matched_sources_1, matched_sources_2, matched_sources_3)
setwd("~/Dropbox/switchers/Matching/data/matches/APR_R&R")
write.table(sources_postdem, "sources_postdem.csv", sep = ",", row.names = FALSE)
